/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;

public class InfoBar {

  private Sash infoBarSash;

  private Layers layers;

  private CommandStack commandStack;

  private StatusBar statusbar;

  private boolean collapsed = false;

  private int width = 200;

  public InfoBar(Ganesa ganesa, Composite parent) {
    this.statusbar = ganesa.getStatusBar();
    createInfoBar(ganesa, parent);
  }

  public void createInfoBar(final Ganesa ganesa, final Composite parent) {
    infoBarSash = new Sash(parent, SWT.VERTICAL | SWT.BORDER);
    layers = new Layers(this, ganesa, parent);
    commandStack = new CommandStack(this, ganesa, parent);
    infoBarSash.addListener(SWT.MouseDoubleClick, new Listener() {
      public void handleEvent(Event event) {
        if (event.type == SWT.MouseDoubleClick) {
          collapsed = !collapsed;
        }
        ganesa.resize();
      }
    });
    infoBarSash.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent event) {
        widgetSelected(event);
      }

      public void widgetSelected(SelectionEvent event) {
        if (!collapsed) {
          width = parent.getClientArea().width - event.x;
          ganesa.resize();
        }
      }
    });
    resize();
  }

  public void resize() {
    Point infoBarSashSize = infoBarSash.computeSize(SWT.DEFAULT, SWT.DEFAULT,
        false);
    int infoBarX = infoBarSash.getParent().getClientArea().width - width;
    if (collapsed) {
      infoBarX = infoBarSash.getParent().getClientArea().width
          - infoBarSashSize.x;
    }
    infoBarSash.setBounds(infoBarX, 0, infoBarSashSize.x, infoBarSash
        .getParent().getClientArea().height
        - statusbar.getHeight());
    layers.resize();
    commandStack.resize();
  }

  public int getWidth() {
    if (collapsed) {
      return infoBarSash.getSize().x;
    } else {
      return width;
    }
  }

  public int getSashWidth() {
    return infoBarSash.getSize().x;
  }

}
